<template>
<div>
<template>

<!-- 添加课程隐藏端口 -->
<el-dialog title="添加课程" :visible.sync="dialogFormVisible">
  <el-form :model="course">
    <el-form-item label="课程名" label-width="120px">
      <el-input v-model="course.courseName" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="课程教学年" label-width="120px">
      <el-input v-model="course.particularYear" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="教学时长" label-width="120px">
      <el-input v-model="course.coursePeriod" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureAddcourse()">添加</el-button>
      <el-button size="small"  type="primary" @click="cancelAddcourse()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 修改课程隐藏端口 -->
    <el-dialog title="修改课程" :visible.sync="updateDialogFormVisible">
  <el-form :model="ucourse">
    <el-form-item label="课程名" label-width="120px">
      <el-input v-model="ucourse.courseName" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="课程教学年" label-width="120px">
      <el-input v-model="ucourse.particularYear" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="教学时长" label-width="120px">
      <el-input v-model="ucourse.coursePeriod" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureUpdatecourse()">修改</el-button>
      <el-button size="small"  type="primary" @click="cancelUpdatedcourse()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 页面主体 -->
   <el-row :gutter="0">

     <!-- 添加课程按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="addcourse()">增加课程</el-button>
    </el-col>

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="courseDTO.username"
        class="w-50 m-2"
        size="small"
        placeholder="请输入姓名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>
  </el-row>

  <!-- 表格 -->
  <el-table :data="courses" stripe border height="300">
    <el-table-column prop="id" sortable=true label="ID"/>
    <el-table-column prop="courseName" sortable=true label="课程名"/>
    <el-table-column prop="particularYear" sortable=true label="教学年" />
    <el-table-column prop="coursePeriod" sortable=true label="教学学时" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="updatecourse(scope.row)">
                   修改
                </el-button>
                <el-button size="small" type="danger" @click="deletecourse(scope.row.id)">
                   删除
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="courseDTO.capacity"
    :current-page.sync="courseDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '课程',
      // 表格映射对象
      courses: [],
      // 修改的对象
      ucourse: {
        username: '',
        password: '',
        roleId: '',
        courseNo: ''
      },
      // 表格查询对象
      courseDTO: {
          index: 1,
          capacity: 10,
          username: ''
      },
      // 角色列表
      roles: [],
      // 添加课程弹出控制
      dialogFormVisible: false,
      // 修改课程弹出控制
      updateDialogFormVisible: false,
      // 添加课程实体
      course: {
          username: ' ',
          password: '',
          roleId: '',
          courseNo: ''
      },
      // 课程列表总数
      count: 0,
      departments: [],
      classes: [],
    }
  },
  // 页面初始化函数 
  created () {
    this.$http({
        url: "/course",
        params: {
            courseName: this.courseDTO.username
        }
    }).then(response => {
        console.log(response.data)
       this.courses = response.data
    })
    this.$http({
        url: "/classes",
        params: {
            classesName:''
        }
    }).then(response => {
       this.classes = response.data
    })
    this.$http({
        url: "/department",
        params: {
            departmentName:''
        }
    }).then(response => {
       this.departments = response.data
    })
  },
  methods: {
    // 改变当前页
    changeCurrent() {
      findAll()
    },
    // 添加课程
     addcourse() {
       this.dialogFormVisible=true;
     },
     updatecourse(data) {
       this.ucourse=data
       this.updateDialogFormVisible=true
     },
     deletecourse(id) {
        this.$http.delete('/course/' + id).then(response => {
          this.$message({
          message: '删除课程成功',
          type: 'success'
          });
          this.findAll()
        })
     },
     ensureAddcourse() {
      this.$http.post("/course", this.course).then(response => {
        this.$message({
          message: '添加课程成功',
          type: 'success'
        });
        this.clearcourse()
        this.findAll()
      })
    },
    // 取消添加课程
    cancelAddcourse() {
      this.clearcourse()
    },
    ensureUpdatecourse() {
      this.$http.put('/course', this.ucourse).then(response => {
        this.$message({
          message: '修改课程成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.findAll()
      })
    },
    cancelUpdatedcourse() {
      this.updateDialogFormVisible=false
    },
    // 搜索对象，并映射
    search() {
      this.findAll()
    },
    // 查询所有，并映射
    findAll() {
    this.$http({
        url: "/course",
        params: {
            courseName: this.courseDTO.username
        }
    }).then(response => {
        console.log(response.data)
       this.courses = response.data
    })
    },
    // 清除课程，并关闭窗口
    clearcourse() {
        this.course.username=''
        this.course.password=''
        this.course.roleId=''
        this.course.courseNo=''
        this.dialogFormVisible=false
     }
  }
}
</script>
<style>

</style>